/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-unused-vars */
import { MockMethod } from 'vite-plugin-mock'
import {
  resultError,
  resultPageSuccess,
  resultSuccess,
  getRequestToken,
  requestParams,
  baseUrl
} from '../_util'
export function createFakeUserList() {
  return [
    {
      userId: '1',
      username: 'admin',
      realname: '管理员',
      avatar: 'https://q1.qlogo.cn/g?b=qq&nk=190848757&s=640',
      desc: 'manager',
      password: '123456',
      token: 'fakeToken1',
      homePath: '/dashboard/analysis',
      roles: [
        {
          roleName: 'Super Admin',
          value: 'super'
        }
      ]
    },
    {
      userId: '2',
      username: 'user',
      password: '123456',
      realname: '测试用户',
      avatar: 'https://q1.qlogo.cn/g?b=qq&nk=339449197&s=640',
      desc: 'tester',
      token: 'fakeToken2',
      homePath: '/dashboard/workbench',
      roles: [
        {
          roleName: 'Tester',
          value: 'test'
        }
      ]
    }
  ]
}

const fakeCodeList: any = {
  '1': ['1000', '3000', '5000'],

  '2': ['2000', '4000', '6000']
}

//随机生成正则匹配的手机号
const fakePhone = /^1[3456789]\d{9}$/
const userList = (() => {
  const result: any[] = []
  for (let index = 0; index < 10; index++) {
    result.push({
      id: /^1[000][1-9]\d{4}/, //主键id
      username: '@first', //账号
      realname: '@cword(2,6)', //真实姓名
      departIds: '21001,1', //所属部门id
      departIds_dictText: '华南分部,市场部', //所属部门id_dictText
      avatar: '@Image("80*80")', //头像
      birthday: '@datetime', //生日
      sex: '2',
      sex_dictText: '女',
      email: '11111@163.com', //邮箱
      phone: fakePhone, //手机号
      workNo: '200', //工号

      status: '1',
      status_dictText: '正常',

      createBy: 'admin', //创建人
      createTime: '@datetime', //创建时间
      updateBy: 'admin',
      updateTime: '@datetime'
    })
  }
  return result
})()

export default [
  // mock user login
  {
    url: `${baseUrl}/sys/user/userRoleList`,
    type: 'get',
    response: () => {
      console.log('userRoleList', userList)
      return resultPageSuccess(1, 10, userList)
    }
  },
  {
    url: `${baseUrl}/sys/user/queryUserRole`,
    type: 'get',
    response: () => {
      const roleIds = ['10000', '10001', '10002']
      return resultSuccess(roleIds)
    }
  },
  {
    url: `${baseUrl}/sys/user/deleteUserRoleBatch`,
    timeout: 1000,
    method: 'delete',
    response: () => {
      return resultError('没有权限，请联系管理员授权')
    }
  },
  {
    url: `${baseUrl}/sys/user/list`,
    timeout: 1000,
    method: 'get',
    response: () => {
      return resultPageSuccess(1, 10, userList)
    }
  },
  {
    url: `${baseUrl}/sys/user/deleteBatch`,
    timeout: 1000,
    method: 'delete',
    response: () => {
      return resultError('没有权限，请联系管理员授权')
    }
  },

  {
    url: `${baseUrl}/sys/user/add`,
    timeout: 1000,
    method: 'post',
    response: () => {
      return resultError('没有权限，请联系管理员授权')
    }
  },
  {
    url: `${baseUrl}/sys/user/edit`,
    timeout: 1000,
    method: 'put',
    response: () => {
      return resultError('没有权限，请联系管理员授权')
    }
  },
  {
    url: `${baseUrl}/sys/user/resetPassword`,
    timeout: 1000,
    method: 'put',
    response: () => {
      return resultError('没有权限，请联系管理员授权')
    }
  },
  {
    url: `${baseUrl}/sys/user/frozenBatch`,
    timeout: 1000,
    method: 'put',
    response: () => {
      return resultError('没有权限，请联系管理员授权')
    }
  },

  // ----------------------------------------------------------
  {
    url: `${baseUrl}/sys/login`,
    timeout: 200,
    method: 'post',
    response: ({ body }) => {
      const { username, password } = body
      const checkUser = createFakeUserList().find(
        (item) => item.username === username && password === item.password
      )
      if (!checkUser) {
        return resultError('Incorrect account or password！')
      }
      const { userId, username: _username, token, realname, desc, roles } = checkUser
      return resultSuccess({
        roles,
        userId,
        username: _username,
        token,
        realname,
        desc
      })
    }
  },
  {
    url: `${baseUrl}/sys/user/getUserInfo`,
    method: 'get',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      if (!token) return resultError('Invalid token')
      const checkUser = createFakeUserList().find((item) => item.token === token)
      if (!checkUser) {
        return resultError('The corresponding user information was not obtained!')
      }
      return resultSuccess(checkUser)
    }
  },
  {
    url: `${baseUrl}/sys/permission/getPermCode`,
    timeout: 200,
    method: 'get',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      if (!token) return resultError('Invalid token')
      const checkUser = createFakeUserList().find((item) => item.token === token)
      if (!checkUser) {
        return resultError('Invalid token!')
      }
      const codeList = fakeCodeList[checkUser.userId]

      return resultSuccess(codeList)
    }
  },
  {
    url: `${baseUrl}/sys/logout`,
    timeout: 200,
    method: 'get',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      if (!token) return resultError('Invalid token')
      const checkUser = createFakeUserList().find((item) => item.token === token)
      if (!checkUser) {
        return resultError('Invalid token!')
      }
      return resultSuccess(undefined, { message: 'Token has been destroyed' })
    }
  },
  {
    url: `${baseUrl}/sys/randomImage/1629428467008`,
    timeout: 200,
    method: 'get',
    response: (_request: requestParams) => {
      const result =
        ''
      return resultSuccess(result)
    }
  }
] as MockMethod[]
